package com.yonyougov.devops.cruxcisupport.userrole;

import com.yonyougov.devops.cruxcisupport.role.RoleEntity;
import com.yonyougov.devops.cruxcisupport.userrole.dto.service.UserPageByRoleRaramLikeDto;
import com.yonyougov.devops.cruxcisupport.userrole.dto.service.UserRoleAddBatchDto;
import com.yonyougov.devops.cruxcisupport.userrole.dto.service.UserRoleByUserParamPageLikeDto;
import com.yonyougov.devops.cruxcisupport.userrole.vo.UserRoleByRoleParamPageLikeVo;
import com.yonyougov.devops.cruxcisupport.userrole.vo.UserRoleByUserParamPageLikeVo;
import com.yonyougov.devops.cruxcisupport.util.CruxDevOpsIdGenerator;
import com.yonyougov.devops.fwc.core.dao.BaseDao;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * t_user_role服务实现
 */
@Service
public class UserRoleServiceImpl extends BaseServiceImpl<UserRoleEntity, BigInteger> implements UserRoleService {
    private final UserRoleDao userRoleDao;

    public UserRoleServiceImpl(UserRoleDao userRoleDao) {
        this.userRoleDao = userRoleDao;
    }

    @Override
    public BaseDao<UserRoleEntity, BigInteger> getDao() {
        return userRoleDao;
    }

    //<!--用户自定义代码开始-->

    /**
     * 根据用户方面的参数查询关联关系(分页+模糊)
     *
     * @param userParamPageLikeDto 用户方面的参数
     * @return Pagination<UserPageLikeVo>
     */
    @Override
    public Pagination<UserRoleByUserParamPageLikeVo> findByUserParamPageLike(UserRoleByUserParamPageLikeDto userParamPageLikeDto) {

        List<UserRoleByUserParamPageLikeVo> userRolePageLikeVoList = userRoleDao.findByUserParamPageLike(userParamPageLikeDto);
        int totalCount = userRoleDao.countByUserParamPageLike(userParamPageLikeDto);

        return new Pagination<>(userParamPageLikeDto.getMax(), userParamPageLikeDto.getPageNum(), totalCount, userRolePageLikeVoList);
    }

    /**
     * 根据用户id查询所关联的角色列表
     *
     * @param userId 用户id
     * @return 角色实体
     */
    @Override
    public List<RoleEntity> findAllByUserId(BigInteger userId) {
        return userRoleDao.findRolesByUserId(userId);
    }

    /**
     * 根据主键批量删除 用户和角色的关联关系
     *
     * @param ids 主键集合
     */
    @Override
    public void deleteBatchUserRoleByIds(List<BigInteger> ids) {
        userRoleDao.deleteBatchUserRoleByIds(ids);
    }

    /**
     * 根据角色方面的参数查询关联关系(分页+模糊)
     *
     * @param roleParamPageLikeDto 角色方面的参数
     * @return Pagination<UserPageLikeVo>
     */
    @Override
    public Pagination<UserRoleByRoleParamPageLikeVo> findByRoleParamPageLike(UserPageByRoleRaramLikeDto roleParamPageLikeDto) {

        List<UserRoleByRoleParamPageLikeVo> userPageLikeList = userRoleDao.findByRoleParamPageLike(roleParamPageLikeDto);
        int totalCount = userRoleDao.countByRoleParamPageLike(roleParamPageLikeDto);

        return new Pagination<UserRoleByRoleParamPageLikeVo>(roleParamPageLikeDto.getMax(), roleParamPageLikeDto.getPageNum(), totalCount, userPageLikeList);
    }

    /**
     * 用户批量关联角色
     *
     * @param userRoleAddBatchDto 批量关联Dto
     */
    @Override
    public void addMulUserRole(UserRoleAddBatchDto userRoleAddBatchDto) {
        List<UserRoleEntity> userRoleEntityList = new ArrayList<>();
        userRoleAddBatchDto.getRoleId().forEach(roleId -> {
            UserRoleEntity userRoleEntity = new UserRoleEntity();
            userRoleEntity.setId(new CruxDevOpsIdGenerator().generatorId(userRoleDao));
            userRoleEntity.setUserId(userRoleAddBatchDto.getUserId());
            userRoleEntity.setRoleId(roleId);
            userRoleEntityList.add(userRoleEntity);
        });
        userRoleDao.addBatch(userRoleEntityList);
    }

    //<!--用户自定义代码结束-->
}
